﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using miniword.compiler;
using miniword.log;

namespace miniword
{

    public partial class MainInterface : Form
    {
        private const string TAG = "MainInterface";

        public const string fileType = "IDL文件（*.idl）|*.idl|ASN1文件（*.asn1）|*.asn1";

        private int childFormNumber = 0;

        private search searchDlg = null;

        public MainInterface()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Editor();
            childForm.MdiParent = this;
            childForm.Text = "窗口 " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            string fileName;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
                foreach (Editor form in this.MdiChildren)
                {
                    if (fileName == form.Text)
                    {
                        form.Visible = true;
                        form.Activate();
                        return;
                    }
                }
                try
                {
                    Editor form = new Editor(this,fileName);
                    childFormNumber++;
                    form.Show();
                }
                catch (ArgumentException)
                {
                    Log.d(TAG, "Fail to open the file, OpenFile get ArgumentException");
                    MessageBox.Show("Fail to open the file");
                }
            }
            else
            {
                Log.d(TAG, "Fail to open the file, openFileDialog1.ShowDialog is not OK");
                MessageBox.Show("Fail to open the file");
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileName;
            Editor form = (Editor)this.ActiveMdiChild;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                saveFileDialog1.FileName = form.Text;
                fileName = saveFileDialog1.FileName;
                saveFileDialog1.Filter = fileType;
                form.save_as(fileName);
                MessageBox.Show("Save the file successfully");
            }
            else
            {
                MessageBox.Show("Fail to save the file");
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFile((Editor)this.ActiveMdiChild);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            this.saveToolStripMenuItem_Click(sender,e);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            foreach (Editor form in this.MdiChildren)
            {
                saveToolStripMenuItem_Click(sender,e);
            }
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;

            if (form != null)
                form.undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;

            if (form != null)
                form.redo();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;

            if (form != null)
                form.selectAll();
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;
            form.printPreview();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;
            form.print();
        }

        private void compileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void printPreviewToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void findToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (searchDlg != null)
                searchDlg.Close();
            searchDlg = new search();
            searchDlg.Owner = this;
            searchDlg.Show();
        }

        private void aSN1CCompilerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;
            saveFile(form);
            Compiler.asn1cCompiler(form.get_file_name());
        }

        private void iDLComplierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor form = (Editor)this.ActiveMdiChild;
            saveFile(form);
            Compiler.idlCompiler(form.get_file_name());
        }

        private void saveFile(Editor form)
        {
            string file_name = "";
   
            if (form != null && (form.get_file_name() != ""))
            {
                saveFileDialog1.FileName = form.Text;
                form.save(file_name);
            }
            else if (form != null && (form.get_file_name() == "" && saveFileDialog1.ShowDialog() == DialogResult.OK))
            {
                file_name = saveFileDialog1.FileName;
                saveFileDialog1.FileName = form.Text;
                form.save(file_name);
                MessageBox.Show("Save the file successfully");
            }
            else
            {
                MessageBox.Show("Fail to save the file");
            }
        }
    }
}
